**********************
***  PREDICTIONS *****
**********************

capt drop _est_*


******************************
***  Species growth rate *****
******************************

***  Get coeff from polynominal
preserve
use "${dataOut}Poly_PPR_K", clear
gen stuff_present =  strpos(parm, "ppr_implemented") > 0
keep if stuff_present==1
keep parm estimate
gen N=_n
reshape wide estimate, i(parm) j(N)
egen b1=max(estimate1)
egen b2=max(estimate2)
egen b3=max(estimate3)
keep b*
gen N=_n
keep if N==1
tempfile helpfile
save `helpfile'
restore
***


** Calculate effect of PPR for sample ****
preserve

gen N=1
sort N
merge m:m N using `helpfile'
drop _merge N

*** only keep PPR!
keep if $TreatmentGroup==0
keep if iyear>=2000 & iyear<=2006  /*Only keep 6 last years...*/
drop if K==.
drop if K==0

collapse (mean) K b*, by(id_eez_tax eez_id eez_name)

* Bound the values of K
drop if K==.
drop if K==0
replace K=0.05 if K<0.05
replace K=0.50 if K>0.50

*** make calculations
gen PPR_NetEffect=b1 + (b2*K) + (b3*K*K)



** Save predictions per  id_eez_tax
** Used for figures in Appendix
gen PPR_K=PPR_NetEffect 
save  "${dataOut}PPR_K_id_eez_tax" , replace
drop PPR_K



** Average over EEZs
collapse (mean) PPR_NetEffect, by(eez_id eez_name)


* Check for duplicates
tab eez_id
sort eez_id
bysort eez_name: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
drop dup
drop if eez_id==258
sort eez_id
bysort eez_id: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0


** Average over EEZs
collapse (mean) PPR_NetEffect, by(eez_id)

sort eez_id
merge m:m  eez_id using "${data}EEZ_boundries_db_corrected"
tab eez_id if _merge==1

gen PPR_NetEffectR=round(PPR_NetEffect,.001)

keep PPR_NetEffectR eez_id 
rename PPR_NetEffectR PPR_K

tempfile PPR_K
save `PPR_K'


restore
**********************************


******************************
****    Species price    *****
******************************

preserve
use "${dataOut}Poly_PPR_Price" ,clear
gen stuff_present =  strpos(parm, "ppr_implemented") > 0
keep if stuff_present==1
keep parm estimate
gen N=_n
reshape wide estimate, i(parm) j(N)
sum
egen b1=max(estimate1)
egen b2=max(estimate2)
egen b3=max(estimate3)
keep b*
gen N=_n
keep if N==1
tempfile helpfile
save `helpfile'
restore
**

**
preserve

bysort taxonkey: egen hvar=mean(realprice_taxon) if iyear>=$PriceYearStart & iyear<=$PriceYearEnd
bysort taxonkey: egen helpvar=mean(hvar)

drop if helpvar==0
drop if helpvar==.

gen N=1
sort N
merge m:m N using `helpfile'
drop _merge N

*** only keep PPR
keep if $TreatmentGroup==0
keep if iyear>=2000 & iyear<=2006  /*Only keep 6 last years...*/

collapse (mean) helpvar b*, by(id_eez_tax eez_id eez_name)

* NBNB Bound the values of Price
sum helpvar
drop if helpvar<=0
drop if helpvar==.

replace helpvar=5000 if helpvar>5000

*** make calculations
gen PPR_NetEffect= ///
   b1 ///
+ (b2*helpvar) ///
+ (b3*helpvar*helpvar)


** Save predictions per  id_eez_tax
** Used for figures in Appendix
gen PPR_Price=PPR_NetEffect 
save  "${dataOut}PPR_Price_id_eez_tax" , replace
drop PPR_Price


collapse (mean) PPR_NetEffect, by(eez_id eez_name)

tab eez_id
sort eez_id
bysort eez_name: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
drop dup
drop if eez_id==258
sort eez_id
bysort eez_id: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0

collapse (mean) PPR_NetEffect, by(eez_id)
sort eez_id
merge 1:1  eez_id using "${data}EEZ_boundries_db_corrected"
tab eez_id if _merge==1

gen PPR_NetEffectR=round(PPR_NetEffect,.001)

keep PPR_NetEffectR eez_id 
rename PPR_NetEffectR PPR_Price
//save "${dataOut}PPR_Price" , replace

tempfile PPR_Price
save `PPR_Price'

restore
**********************************




******************************
****   GDP per capita    *****
******************************


***  Get coeff from polynominal
preserve
use "${dataOut}Poly_PPR_GDP", clear
gen stuff_present =  strpos(parm, "ppr_implemented") > 0
keep if stuff_present==1
keep parm estimate
gen N=_n
reshape wide estimate, i(parm) j(N)
sum
egen b1=max(estimate1)
egen b2=max(estimate2)
egen b3=max(estimate3)
egen b4=max(estimate4)
egen b5=max(estimate5)
keep b*
gen N=_n
keep if N==1
tempfile helpfile
save `helpfile'
restore
****


***
preserve

gen h1=gdp_capita_c /1000
sum h1
bysort id_eez_tax: egen h2=mean(h1) if iyear>=$PriceYearStart & iyear<=$PriceYearEnd
bysort id_eez_tax: egen helpvar=max(h2)
drop if helpvar==0
drop if helpvar==.

gen N=1
sort N
merge m:m N using `helpfile'
drop _merge N

*** only keep non-PPR
keep if $TreatmentGroup==0
keep if iyear>=2000 & iyear<=2006  /*Only keep 2000s...*/

collapse (mean) helpvar b*, by(id_eez_tax eez_id eez_name)


* Bound the values of GDP
drop if helpvar==.
drop if helpvar==0
replace helpvar=20 if helpvar<20
replace helpvar=34 if helpvar>34


*** make calculations
gen PPR_NetEffect= ///
   b1  ///
+ (b2*helpvar) ///
+ (b3*helpvar*helpvar) ///
+ (b4*helpvar*helpvar*helpvar) ///
+ (b5*helpvar*helpvar*helpvar*helpvar) ///


collapse (mean) PPR_NetEffect, by(eez_id eez_name)

tab eez_id
sort eez_id
bysort eez_name: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
drop dup
drop if eez_id==258
sort eez_id
bysort eez_id: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
collapse (mean) PPR_NetEffect, by(eez_id)
sort eez_id
merge 1:1  eez_id using "${data}EEZ_boundries_db_corrected"
tab eez_id if _merge==1

gen PPR_NetEffectR=round(PPR_NetEffect,.001)

keep PPR_NetEffectR eez_id 
rename PPR_NetEffectR PPR_GDP
//save "${dataOut}PPR_GDP" , replace
tempfile PPR_GDP
save `PPR_GDP'

restore
**********************************



*************************************************
**** Predictions: Trade openness - Non-PPR ******
*************************************************

preserve
use "${dataOut}Poly_PPR_Trade", clear
gen stuff_present =  strpos(parm, "ppr_implemented") > 0
keep if stuff_present==1
keep parm estimate
gen N=_n
reshape wide estimate, i(parm) j(N)
sum
egen b1=max(estimate1)
egen b2=max(estimate2)
egen b3=max(estimate3)
egen b4=max(estimate4)
egen b5=max(estimate5)
keep b*
gen N=_n
keep if N==1
tempfile helpfile
save `helpfile'
restore
****


****
preserve

gen h1=trade
bysort id_eez_tax: egen h2=mean(h1)  if iyear>=$PriceYearStart & iyear<=$PriceYearEnd 
bysort id_eez_tax: egen helpvar=mean(h2)

gen N=1
sort N
merge m:m N using `helpfile'
drop _merge N

***** only keep non-PPR! ****
keep if $TreatmentGroup==0
keep if iyear>=2000 & iyear<=2006 /*NBNB*/
drop if eez==0  /*High seas*/
drop if eez==.  
drop if helpvar==.
drop if helpvar==0

collapse (mean) helpvar  b* , by(eez_id eez_name)

rename helpvar trade

* Bound the values of trade
drop if trade==.
drop if trade==0

replace trade=30 if trade<30
replace trade=90 if trade>90


*** make calculations
gen PPR_NetEffect= ///
   b1  ///
+ (b2*trade) ///
+ (b3*trade*trade) ///
+ (b4*trade*trade*trade) ///
+ (b5*trade*trade*trade*trade) 

collapse (mean) PPR_NetEffect, by(eez_id eez_name)

tab eez_id
sort eez_id
bysort eez_name: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
drop dup
drop if eez_id==258
sort eez_id
bysort eez_id: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0

collapse (mean) PPR_NetEffect, by(eez_id)
sort eez_id
merge 1:1  eez_id using "${data}EEZ_boundries_db_corrected"
tab eez_id if _merge==1

gen PPR_NetEffectR=round(PPR_NetEffect,.001)

keep PPR_NetEffectR eez_id 
rename PPR_NetEffectR PPR_Trade
//save "${dataOut}PPR_Trade" , replace
tempfile PPR_Trade
save `PPR_Trade'

restore
**********************************




*************************************************
*****  Predictions: ROL - High/Low **************
*************************************************



*******
preserve
use "${dataOut}Poly_High_Low_ROL", clear
gen stuff_present =  strpos(parm, "ppr_implemented") > 0
keep if stuff_present==1
keep parm estimate
gen N=_n
reshape wide estimate, i(parm) j(N)
sum
egen b1=max(estimate1)
egen b2=max(estimate2)
keep b*
gen N=_n
keep if N==1
tempfile helpfile
save `helpfile'
restore
****


***
preserve
drop if law_average==.
gen h1=law_average
bysort id_eez_tax: egen helpvar=mean(h1)
xtile percentile = helpvar, nq(10)
gen HighROL=0
replace HighROL=1 if percentile>=10
drop h1 helpvar percentile
drop if law_average==.

gen N=1
sort N
merge m:m N using `helpfile'
drop _merge N

*** only keep PPR!
keep if $TreatmentGroup==0
keep if iyear>=2000 & iyear<=2006  

collapse (mean) High* b*, by(id_eez_tax eez_id eez_name)

* Calculate net effect
gen PPR_NetEffect = b1 + (b2*HighROL) 

collapse (mean) PPR_NetEffect, by(eez_id eez_name)
tab eez_id
sort eez_id
bysort eez_name: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
drop dup
drop if eez_id==258
sort eez_id
bysort eez_id: gen dup = cond(_N==1,0,_n)
tab dup
tab eez_name if dup!=0
collapse (mean) PPR_NetEffect, by(eez_id)
sort eez_id
merge 1:1  eez_id using "${data}EEZ_boundries_db_corrected"
tab eez_id if _merge==1

gen PPR_NetEffectR=round(PPR_NetEffect,.001)

keep PPR_NetEffectR eez_id 
rename PPR_NetEffectR PPR_ROL_High_Low
//save "${dataOut}PPR_ROL_High_Low" , replace
tempfile PPR_ROL_High_Low
save `PPR_ROL_High_Low'


restore
**********************************



**********************************
***   Combine all datasets *******
**********************************


preserve
drop if eez_id==.
keep if $TreatmentGroup==0
keep if iyear>=2000

collapse (max) TreatEEZ ///
EuropeNorthAm CentralAsiaEasternEurope SouthEastAsia LatinAmerica Pacific Caribbean ///
Africa Oceania* MiddleEast HighSeas ///
(mean)  highly_migratory K  law_average trade, by(id_eez_tax eez_id   eez_name  region regionNew9)

collapse (max) TreatEEZ ///
EuropeNorthAm CentralAsiaEasternEurope SouthEastAsia LatinAmerica Pacific Caribbean ///
Africa Oceania* MiddleEast HighSeas ///
(mean) highly_migratory K  law_average trade, by(eez_id   eez_name region regionNew9)

sort eez_id
tempfile helpfile
save `helpfile'
restore


preserve

use  `PPR_Trade', clear
sort eez_id
merge m:m  eez_id using `PPR_K'
drop _merge
sort eez_id
drop if eez_id==.
merge 1:1  eez_id using `PPR_Price'
drop _merge
sort eez_id
merge 1:1  eez_id using `PPR_GDP'
drop _merge
sort eez_id
merge 1:1  eez_id using `PPR_ROL_High_Low'
drop _merge
sort eez_id

egen PPR_Mean_K_Price				 =rowmean(PPR_K PPR_Price )
egen PPR_Mean_Trade_GDP_ROLH		 =rowmean(PPR_Trade PPR_GDP PPR_ROL_High_Low)
egen PPR_Mean_K_Price_Trade_GDP_ROLH =rowmean(PPR_K PPR_Price PPR_Trade PPR_GDP PPR_ROL_High_Low  )

sort eez_id
merge m:m  eez_id using "${data}\EEZ_boundries_db_corrected"
sum
drop _merge

sort eez_id
merge 1:1  eez_id using `helpfile' 
drop _merge
sum

* NBNB: Replace average measure by . if mising data for one the dimensions
replace PPR_Mean_K_Price_Trade_GDP_ROLH=. if PPR_Mean_K_Price==. | PPR_Mean_Trade_GDP_ROLH==.
 

** Make maps
drop if eez_id==.

*NBNB: Drop if missing value
drop if PPR_Trade==.
drop if PPR_ROL==. 
drop if PPR_GDP==.
drop if PPR_K==. 
drop if PPR_Price==.

/*
/* Dataset for ArcGIS*/
export excel using "${ArcGIS}Map_Predictions_20180917.xls", ///
firstrow(variables) replace
*/

/* Dataset for QGIS - csv*/
export delimited using "${QGIS}Map_Predictions_20180917.csv", replace


* STATA dataset
save "${dataOut}PPR_Predictions" , replace

restore
************

capt drop _est_*

